package com.goxueche.lib_core.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.fragment.app.FragmentActivity;

import com.goxueche.lib_core.ui.mvp.view.IBaseView;
import com.goxueche.lib_core.utils.AndroidUtils;
import com.goxueche.lib_core.widgets.Alert;
import com.trello.rxlifecycle4.LifecycleTransformer;
import com.trello.rxlifecycle4.android.ActivityEvent;

/**
 * @author 李澄锋<br>
 * 2018/8/13
 */
public abstract class FrameBaseActivity extends SupportActivity implements Handler.Callback, IBaseView {

    // 推荐用方法访问该变量 getContext()
    private FragmentActivity context;
    protected Dialog dialog = null;
    protected boolean isResume = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        ActivityMgr.push(this);
        setContentView(layoutId());
        initImmersionBar();
        initView();
        initData();
    }


    protected abstract int layoutId();

    /**
     * 注意子该方法之前需要先调用 {@link #setContentView(int)}
     */
    protected abstract void initView();

    protected abstract void initData();

    /**
     * 沉浸式状态栏
     */
    public abstract void initImmersionBar();

    @Override
    public FragmentActivity getContext() {
        return context;
    }

    public void showToast(final String message) {
        Alert.toast(message);
    }

    public void showToast(final int messageId) {
        Alert.toast(messageId);
    }

    @Override
    public void startActivity(Intent intent) {
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        super.startActivity(intent);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        super.startActivityForResult(intent, requestCode);
    }

    /**
     * 处理各个Activity的请求message
     *
     * @param msg
     * @return 如果返回true则该消息会停止继续分发<br>
     * {@link HandlerMgr#sendMessage(int, int, Object, int...)}
     */
    @Override
    public boolean handleMessage(Message msg) {

        return false;
    }

    @Override
    public void showLoading() {
        showWaitDialog(true);
    }

    /**
     * @param cancelable 是否支持取消操作
     * @Description: 显示加载对话框
     */
    public abstract Dialog showWaitDialog(boolean cancelable);

    @Override
    public void dismissLoading() {
        dissmissWaitingDialog();
    }

    /**
     * @Description: 显示异步交互后的数据, 隐藏加载框
     */
    public void dissmissWaitingDialog() {
        try {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (dialog != null &&
                            dialog.isShowing() &&
                            !isFinishing() &&
                            !isDestroyed()) {
                        dialog.dismiss();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        Alert.init(getApplication());
    }

    @Override
    protected void onResume() {
        super.onResume();
        isResume = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isResume = false;
    }

    @Override
    protected void onStop() {
        dissmissWaitingDialog();
        super.onStop();
    }

    /**
     * 用来 绑定view 生命周期，解决rxjava内存泄露
     *
     * @return LifecycleTransformer
     */
    @Override
    public <T> LifecycleTransformer<T> getLifecycleTransformer() {
        return bindUntilEvent(ActivityEvent.DESTROY);
    }

    @Override
    protected void onDestroy() {
        ActivityMgr.remove(this);
        AndroidUtils.hideSoftInput(getContext());
        super.onDestroy();
    }

}
